package com.ma.controller;

import com.ma.Utils.filesUtil;
import com.ma.entity.Files;
import com.ma.entity.Message;
import com.ma.entity.Project;
import com.ma.entity.Student;
import com.ma.server.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ProjectName: project
 * @Package: com.ma.controller
 * @ClassName: ProjectCon
 * @Author: YHHHH
 * @Description:
 * @Date: 2021/3/29 23:20
 * @Version: 1.0
 */
@RestController
@CrossOrigin
public class ProjectCon {
    @Autowired
    private ProjectServer projectServer;
    @Autowired
    private TeacherServer teacherServer;
    @Autowired
    private LoginServer loginServer;
    @Autowired
    private StudentServer studentServer;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private teamServer teamServer;
    @Autowired
    private MessageServe messageServe;
    @Autowired
    private FilesServer filesServer;
    @Autowired
    private filesUtil filesUtil;



    @GetMapping("/project/teacherProject")
    public List<Project> findAll(@RequestParam("teacherId")String id){
        int teacherId = Integer.parseInt(id);
        List<Project> teacherProject = projectServer.teacherProject(teacherId);
        return teacherProject;
    }

    /*
    新增项目
     */
    @PostMapping("/project/addProject")
    public String addProject(@RequestBody Project project){
        Integer teacherId = project.getTeacherId();
        Integer majorId = teacherServer.findTeacherMajor(teacherId);
        project.setMajorId(majorId);
        System.out.println(project);
        Integer integer = projectServer.addProject(project);
        return "dasdwadwad";
    }

    /*
    查看项目详情
     */
    @GetMapping("/project/showProject")
    public Project showProject(@RequestParam("projectId")Integer id){
        System.out.println(id);
        Project project = projectServer.showProject(id);
        return project;
    }
    /*
    查出所有的课题
     */
    @GetMapping("/project/allProject")
    public List<Project> allProject(@RequestParam("number")String  number){
        Integer major = studentServer.findMajor(number);
        List<Project> projects = projectServer.allProject(major);
        return projects;
    }
    /*
    选择课题
     */
    @GetMapping("project/joinProject")
    @Transactional
    public String joinProject(@RequestParam("projectId")Integer id,@RequestParam("number")String number){
        redisTemplate.setStringSerializer(new StringRedisSerializer());
        System.out.println(id);
        System.out.println(number);
        Integer teamID = studentServer.findTeamID(number);      //找到该学生的组号
        System.out.println("teamID="+teamID);
        if (teamID==null){
            return "还没加入小组，请加入小组或者创建小组！";
        }
        Integer project = teamServer.findProject(teamID);       //找到该小组的项目号
        if(project!=null){
            return "你的小组已经选择了项目!";
        }
        Boolean hasKey = redisTemplate.opsForValue().setIfAbsent(id, teamID, 10, TimeUnit.SECONDS);       //如果有钥匙说明该进程抢到了
        if (hasKey==true){
            Integer teamId = projectServer.findTeamId(id);              //找到项目里的组号
            if (teamId==null){
                Integer integer = teamServer.updateProject(teamID, id);         //把小组的项目号改了
                Integer integer1 = projectServer.updateTeamId(teamID, id);
                redisTemplate.delete(id);
                Student receiveStudent = loginServer.findLoginStudent(number);   //改redis里的数据
                redisTemplate.opsForValue().set(number,receiveStudent);
                //给小组成员发送选择课题成功的消息
                Message message =new Message();
                List<String> allTeammateNum = studentServer.findAllTeammateNum(teamID);//找到组员
                for (String s : allTeammateNum) {
                    message.setReceiveId(s);
                    message.setSendId("00000000");
                    message.setMessage("你的小组成功选择该课题，请尽快开始！");
                    Integer integer2 = messageServe.addMessage(message);
                }
                return "success!";
            }else {
                redisTemplate.delete(id);
                return "该项目已被其他组选择!";
            }
        }else {
            return "当前太多人选择该项目，请重试！";
        }
    }

    /*
    删除课题
     */
    @GetMapping("/project/deleteProject")
    @Transactional
    public String deleteProject(@RequestParam("projectId")Integer id,@RequestParam("teamID")Integer teamId){
        //删除小组的文件
        //如果没有小组 直接删除项目
        if (teamId==0){
            Integer integer = projectServer.deleteProject(id);
            if (integer!=0){
                return "200";
            }
            return "400";
        }
        //如果有小组   改小组的项目号  删除文件  删除项目
        Integer integer = teamServer.updateProject(teamId,null);
        //将文件从内存删除
        List<Files> byNumber = filesServer.findTeamFiles(teamId);
        for (Files files : byNumber) {
            Integer filesId = files.getFilesId();
            filesUtil.filesDelete(filesId);
        }
        Integer integer2 = filesServer.deleteTeamFiles(teamId);     //将文件从数据库删除
        Integer integer3= projectServer.deleteProject(id);            //删除项目
        Integer integer1 = teamServer.updateStudentScore(teamId);       //修改组员的评分
        Integer integer4 = teamServer.updateScore(0, teamId);       //修改小组的评级
        System.out.println(integer1+integer2+integer3+integer4);
       // if(integer!=0&&integer1!=0&&integer2!=0&&integer3!=0&&integer4!=0){
            //更新redis
            List<String> allTeammateNum = studentServer.findAllTeammateNum(teamId);
            System.out.println("123456");
            redisTemplate.delete(allTeammateNum);
            return "200";
       // }
       //return "400";
    }
}
